;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Enterprise.AgentLoginlogs", [])
        .controller("AgentLoginlogsCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/logininfo/agentLoginStatisticsExportAll"),
                allUrlDetail: ybHttp.RequestUrl("device", "/logininfo/agentLoginDetailsExportAll"),
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.pageInfoDetail = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.datePickerFlag = [false, false];

            $scope.condition = {};
            $scope.conditionDetail = {};
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1000,
                tableCol: [
                    {
                        type: "",
                        name: "登录名称",
                        dataField: "loginName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "真实姓名",
                        dataField: "userName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "公司名称",
                        dataField: "companyName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "登录次数",
                        dataField: "loginCount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                        clickFn: "viewDetail"
                    },
                    {
                        name: "登录地区数量",
                        dataField: "loginAreaCount",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                ]
            };
            $scope.tbConfDetail = {
                scrollWidth: 1000,
                tableCol: [
                    {
                        type: "",
                        name: "登录名称",
                        dataField: "loginName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "真实姓名",
                        dataField: "userName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "公司名称",
                        dataField: "companyName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "登录Ip",
                        dataField: "loginIp",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                    },
                    {
                        type: "",
                        name: "Ip归属地",
                        dataField: "ipHome",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                    },
                    {
                        type: "",
                        name: "登录时间",
                        dataField: "loginDateStr",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: "",
                    },
                ]
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };

            function viewDetail(item) {
                $scope.curUserId = item.userId;
                $scope.conditionDetail = {
                    userId: $scope.curUserId
                };
                getDataList4detail().then(function() {
                    $scope.changeTab(1);
                })
            }

            $scope.activeFlag = [true, false];
            $scope.changeTab = function (index) {
                angular.forEach($scope.activeFlag, function (activeFlag) {
                    activeFlag = false;
                });
                $scope.activeFlag[index] = true;
                switch (index) {
                    case 0:
                        $scope.agentUserLogList = [];
                        break;
                    default:
                        break;
                }
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            var btnPermissionCode = {
                export: window.ActionCode.agentLoginlogsExport,
                detailExport: window.ActionCode.agentLoginlogsDetailExport,
            };

            $scope.btnPermission = {
                export: true,
                detailExport: true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/logininfo/agentLoginStatisticsInfo";
                var param = {};
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageSize = $scope.pageInfo.pageSize;
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.search4detail = function (curPage) {
                getDataList4detail(curPage);
            };

            $scope.reset4Detail = function () {
                $scope.conditionDetail = {
                    userId: $scope.curUserId
                };
            };

            function getDataList4detail(curPage) {
                var service = "device";
                var reqUrl = "/logininfo/agentUserLogs";
                var param = {};
                if (!!curPage) {
                    $scope.pageInfoDetail.curPage = curPage;
                }
                param.pageSize = $scope.pageInfoDetail.pageSize;
                param.pageNum = $scope.pageInfoDetail.curPage;
                param.filters = angular.copy($scope.conditionDetail);
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var data = resp.value || {};
                    angular.forEach(data.pageInfoList, function (item) {
                        item.loginDateStr = $filter("YbDate")(item.loginDate,'yyyy-MM-dd HH:mm:ss');
                    });
                    $scope.agentUserLogList = data.pageInfoList;
                    $scope.pageInfoDetail.totalRows = data.pageInfo.total;
                    $scope.pageInfoDetail.totalPages = data.pageInfo.pages;
                });
            }

            (function () {
                getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }]);
})(window, window.angular);